using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
using Microsoft.Devices.Sensors;

namespace WindowsPhoneGame1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        Texture2D ball;
        Vector2 ballPos;
        Vector2 ballSpeedX;
        Vector2 ballSpeedY;
        SpriteFont myFont;
        string counterText;

        int counter = 0;

        Vector2 gravity;
        float maxX;
        float maxY;
        SoundEffect boing;
        Accelerometer accelSensor = new Accelerometer();
        Vector3 accelReading = new Vector3();

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            // Frame rate is 30 fps by default for Windows Phone.
            TargetElapsedTime = TimeSpan.FromTicks(333333);
        }

        public void AccelerometerReadingChanged(object sender, AccelerometerReadingEventArgs e)
        {
            accelReading.X = (float)e.X;
            accelReading.Y = (float)e.Y;
            accelReading.Z = (float)e.Z;
        }


        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft;

            base.Initialize();

            myFont = Content.Load<SpriteFont>("SpriteFont1");

            ballPos.X = 60;
            ballPos.Y = graphics.GraphicsDevice.Viewport.Height / 2 - ball.Height / 2;

            maxX = graphics.GraphicsDevice.Viewport.Width - ball.Width;
            maxY = graphics.GraphicsDevice.Viewport.Height - ball.Height;


            ballSpeedX = new Vector2(0, 0);
            ballSpeedY = new Vector2(0, 0);

            gravity = new Vector2(5000.0f, 0);


            accelSensor.ReadingChanged +=
                new EventHandler<AccelerometerReadingEventArgs>(AccelerometerReadingChanged);

            accelSensor.Start();

        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            ball = Content.Load<Texture2D>("ball");
            boing = Content.Load<SoundEffect>("boing");

        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            bool bInMiddle = true;


            if (bInMiddle)
            {
                if (accelReading.X > .2)
                {
                    ballSpeedY.Y = -300;
                }
                else if (accelReading.X < -.2)
                {
                    ballSpeedY.Y = 300;
                }
                else
                {
                    ballSpeedY.Y = 0;

                }
            }

           
            
            ballSpeedX += gravity * (float)gameTime.ElapsedGameTime.TotalSeconds;
            
            ballPos += ballSpeedX * (float)gameTime.ElapsedGameTime.TotalSeconds;

            ballPos += ballSpeedY * (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (ballPos.X > maxX)
            {
                ballPos.X = maxX - (ballPos.X - maxX);
                ballSpeedX = -ballSpeedX;
                boing.Play();
                counter++;
            }

            counterText = counter.ToString();
            if ((counter > 0) && ((counter % 100) == 0))
                counterText += "    awesome!!!";


            if (ballPos.Y > maxY)
            {
                ballPos.Y = maxY;
                ballSpeedY.Y = 0;
                //boing.Play();
                bInMiddle = false;

            }

            if (ballPos.Y <= 0)
            {
                ballPos.Y = 0;
                ballSpeedY.Y = 0;
                bInMiddle = false;
                //boing.Play();
            }


            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);

            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

            Vector2 counterPos = new Vector2(5, GraphicsDevice.Viewport.Height-20);
            spriteBatch.Draw(ball, ballPos, Color.White);
            spriteBatch.DrawString(myFont, counterText, counterPos, Color.Black, (float)(3 * Math.PI / 2), new Vector2(0, 0), 1, SpriteEffects.None, 1);

            
            spriteBatch.End();
            

            base.Draw(gameTime);
        }
    }
}
